<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Category;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class CategoryController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = (new Category())->tree();
        return view('admin/category/index')->with('data',$data);
    }

    public function changeOrder()
    {
        $input = Input::all();
        $cate = Category::find($input['cate_id']);
        $cate->cate_order = $input['cate_order'];
        $res = $cate->update();
        if($res){
            $data = [
                'status' => 0,
                'msg' => '分类排序更新成功',
            ];
        }else{
            $data = [
                'status' => 1,
                'msg' => '分类排序更新失败,请重新提交',
            ];
        }
        return $data;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cate = Category::where('cate_pid',0)->get();
        return view('admin/category/add')->with('data',$cate);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store()
    {
        $input = Input::except('_token');
        $rules = [
            'cate_name'=>'required',

        ];
        $message = [
            'cate_name.required'=>'分类名称不能为空',
        ];
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){
            $rs = Category::create($input);
            if($rs){
                return redirect('admin/category');
            }else{
                return back()->with('error','数据填写失败,请稍后重试');
            }
        }else{
            return back()->withErrors($validator);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($cate_id)
    {
        $field = Category::find($cate_id);
        $data = Category::where('cate_pid',0)->get();
        return view('admin/category/edit',compact('field','data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($cate_id)
    {
        $input = Input::except('_token','_method');
        $rs = Category::where('cate_id',$cate_id)->update($input);
        if($rs){
            return redirect('admin/category');
        }else{
            return back()->with('error','分类信息更新失败,请稍后重试');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($cate_id)
    {
        $rs = Category::where('cate_id',$cate_id)->delete();
        if($rs){
            $data = [
                'status'=>0,
                'msg'=>'分类删除成功!'
            ];
            }else{
                $data= [
                    'status'=>1,
                    'msg'=>'分类删除失败,请稍后重试!'
                ];
        }
        return $data;
    }
}
